from typing import List

from PyQt5.QtWidgets import QWidget, QToolButton
from PyQt5.QtCore import *

from GUI import MainWindow
from GUI.Widget.ToolBar_design import Ui_ToolBar
from GUI.Widget.ToolButton import ToolButton


class ToolBar(QWidget, Ui_ToolBar):
    toolList: List[ToolButton] = []
    currentTool: ToolButton = None

    # 部件
    mainWindow: MainWindow = None   # 主窗体

    def __init__(self, window):
        super().__init__(window.toolPanel)
        self.setupUi(self)
        self.mainWindow = window
        # print(self.mainWindow)
        self.__initUI()
        self.show()
        # 定位
        self.move(0, 0)
        self.resize(window.toolPanel.size())

    def __initUI(self):
        list = [
            self.toolButton_Pointer,
            self.toolButton_Pen,
            self.toolButton_Move,
            self.toolButton_Shape,
            self.toolButton_Text
        ]
        self.toolList = list
        self.currentTool = list[0]
        for item in list:
            item.setWindowFlags(Qt.FramelessWindowHint)
            item.setIconSize(QSize(64, 64))
            # item.setCheckable(True)
        # 指示工具
        tool = self.toolButton_Pointer
        tool.name = 'pointer'
        tool.setToolTip('pointer')
        # 移动工具
        tool = self.toolButton_Move
        tool.name = 'move'
        tool.setToolTip('move')
        # 画笔
        tool = self.toolButton_Pen
        tool.name = 'pen'
        tool.setToolTip('pen')
        # 文字
        tool = self.toolButton_Text
        tool.name = 'Text'
        tool.setToolTip('Text')
        # 形状
        tool = self.toolButton_Shape
        tool.name = 'shape'
        tool.setToolTip('shape')


    def closeTool(self, tool: ToolButton):
        """
        关闭工具，通常是玩家鼠标点击工具栏工具后，
        发布关闭工具事件     和 工具失去焦距图层
        :return:
        """
        if tool is None:
            return
        tool.setChecked(False)
        win = self.mainWindow
        # 取消选择工具事件
        event = getattr(tool, 'deselectToolEvent', None)
        if event:
            event(tool)
        # 工具取消选择图层事件
        event = getattr(tool, 'toolFocusOffLayerEvent', None)
        if event:
            layer = win.currentLayer
            if layer:
                print(tool.name, '失去焦距', layer.text())
            event(tool, layer)

        # 关闭工具 编辑器控件
        editorBar = getattr(tool, "editor", None)
        if editorBar is not None:
            editorBar.hide()

    def openTool(self, tool):
        """
       打开工具，通常是玩家鼠标点击工具栏工具后，
       同时关闭之前的工具
       发布 工具焦距图层和工具失去焦距图层事件
       :return:
       """
        win = self.mainWindow
        # 检查图层是否保存
        layer = win.currentLayer
        if layer.isEditing:
            layer.aboutSave()
            return

        self.closeTool(self.currentTool)  # close the previous tool
        # self.currentTool.setChecked(False)
        tool.setChecked(True)
        self.currentTool = tool
        # 选择工具事件
        event = getattr(tool, 'selectToolEvent', None)
        if event:
            event(tool)
        # 工具选择图层事件
        event = getattr(tool, 'toolFocusOnLayerEvent', None)
        if event:
            layer = win.currentLayer
            if layer:
                print(tool.name, '焦距', layer.text())
            event(tool, layer)
        # 打开工具面板
        editorBar = getattr(tool, "editor", None)
        if editorBar is not None:
            editorBar.show()
